<?php


class Admin_Model_Games extends Zend_Db_Table_Abstract


{


	/**


	 * Model Games


	 * @author Nguyen Manh Hung


	 */


	


	protected $_name = TABLE_GAMES; 


	protected $_id = 'id';


	





	/**


	 * @author Nguyen Manh HUng


	 * @param array $filters


	 * @param string $sortFeild


	 * @param int $limit


	 * @param int $page


	 */


	public function fetchGames($filters = array(), $sortFeild = null, $limit = null, $page = 1)


	{


		$select = $this->select();


		//add cac filter vao truy van tim kiem


		if(count($filters)>0)


		{


			foreach ($filters as $feild => $keyword){


				if($feild =='name'){


					$select->where('name like?', $keyword.'%')->orWhere('name like?','%'.$keyword.'%')->orWhere('name like?','%'.$keyword);


                                        $select->orWhere('keywords like?', $keyword.'%')->orWhere('keywords like?','%'.$keyword.'%')->orWhere('keywords like?','%'.$keyword)->group('id');


				}else{


					$select->where($feild .' =?', $keyword);


				}


			}


		}


		//add sort vao truy van


		if(null != $sortFeild)


		{


			$select->order($sortFeild);


		}


		//add limit - offset vao truy van		


		if(null != $limit)


		{


			$offset = $page * NUM_PER_PAGE;


			if($offset<=$limit)


			{


				$offset = $limit+NUM_PER_PAGE;


			}


			$select->limit($limit, $offset);


		}	


		return $this->fetchAll($select);


	}


	public function fetchGamesSlider($filters = array(), $sortFeild = null, $limit = null, $page = 1)
	{

		$select = $this->select();

		//add cac filter vao truy van tim kiem
		if(count($filters)>0)
		{

			foreach ($filters as $feild => $keyword){
				$select->where($feild .' =?', $keyword);
			}
		}

		//add sort vao truy van

		if(null != $sortFeild)
		{
			$select->order($sortFeild);
		}
		$select->limit($limit, 0);
		return $this->fetchAll($select);


	}
	


	public function searchGames($cat_id, $name_game = '', $hot = 0){


            $select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART);


            $select->setIntegrityCheck(false)


           ->joinInner(TABLE_CAT_GAME, TABLE_CAT_GAME.'.game_id = '.$this->_name.'.id',array('cat_id'));  


	    if ($cat_id){


	    	$select->where(TABLE_CAT_GAME.'.cat_id = ?', $cat_id);


	    }  


            if($name_game){


       		$select->where($this->_name.'.name = ?', $name_game);


            }    


       	


            if ($hot) {


       		$select->where($this->_name.'.game_hot = ?', 1);


            }


            $rows = $this->fetchAll($select);


            $rows = $rows->toArray();


            return $rows;


	}


	


	/**


	 * @author Nguyen manh HUng


	 * @param int $id


	 * @param int $order


	 * @return number|number


	 */


	public function updateOrder($id, $order=0){


		$db = Zend_Db_Table::getDefaultAdapter();


		$row = $this->find($id)->current();


		if($row){


			$row->order = $order;


			$row->save();


			return 1;


		}else{


			return 0;


		}


	}


	


	/**


	 * @author Nguyen manh HUng


	 * @param int $id


	 * @param int $flag


	 * @return number|number


	 */


	public function updateFlag($id, $flag=0){


		$db = Zend_Db_Table::getDefaultAdapter();


		$row = $this->find($id)->current();


		if($row){


			$row->flag = $flag;


			$row->save();


			return 1;


		}else{


			return 0;


		}


	}


	


	public function insertGame($nameid,$name,$desc,$content="",$keywords,$instructions="",$width,$height,$type="SWF",$order,$active,$source_image,$source_fileurl,$sys_image,$sys_game,$source_url='', $type_game = 0){


		$db = Zend_Db_Table::getDefaultAdapter();


		$row = $this->createRow();


		$row->nameid = $nameid;


		$row->name = $name;


		$row->desc = $desc;


		$row->content = $content;


		$row->keywords = $keywords;


		$row->instructions = $instructions;


		$row->width = $width;


		$row->height = $width;


		$row->type = $type;


		$row->order = $order;


		$row->active = $active;


		$row->source_image = $source_image;


		$row->source_fileurl = $source_fileurl;


		$row->source_url = $source_url;


		$row->sys_image = $sys_image;


		$row->type_game = $type_game;


		$row->sys_game = $sys_game;


		$row->save();


		$id =$row->id;


		return $id;


	}


	


        public function updateNameID($id, $nameid) {


            $db = Zend_Db_Table::getDefaultAdapter();


            $row = $this->find($id)->current();


            if($row){


                $row->nameid = $nameid;


                $row->save();


                return 1;


            }else {


                return 0;


            }


        }


        public function updateGame($id, $nameid,$name,$desc,$content="",$keywords,$instructions="",$width,$height,$type="SWF",$order,$active,$source_image,$source_fileurl,$sys_image,$sys_game, $source_url='', $type_game = 0, $flag=0){


		$db = Zend_Db_Table::getDefaultAdapter();


		$row = $this->find($id)->current();


		if($row){


			$row->nameid = $nameid;


			$row->name = $name;


			$row->desc = $desc;


			$row->content = $content;


			$row->keywords = $keywords;


			$row->instructions = $instructions;


			$row->width = $width;


			$row->height = $width;


			$row->type = $type;


			$row->order = $order;


			$row->active = $active;


			$row->source_image = $source_image;


			$row->source_fileurl = $source_fileurl;


			$row->source_url = $source_url;


			$row->sys_image = $sys_image;


			$row->sys_game = $sys_game;


			$row->type_game = $type_game;


			$row->flag = $flag;


			$row->save();


			return 1;


		}else {


			return 0;


		}


	}


}


